#include <iostream>
#include <unistd.h>
#include <wait.h>
#include "task.h" 
#include <functional>
#include <vector>
using namespace std;

typedef std::function<void()> task_t;

void LoadTask(std::vector<task_t> &tasks)
{
    tasks.push_back(PrintLog);
    tasks.push_back(Download);
    tasks.push_back(Backup);
}

int main()
{
    pid_t id = fork();
    vector<task_t> tasks;
    LoadTask(tasks);
    if(id == 0)
    {
        while(1)
        {
            printf("我是子进程，pid:%d\n",getpid());
            for(auto &task : tasks)
            {
                task();
            }
            sleep(1);
        }
        exit(1);
    }
    else 
    {
        int status;
        while(1)
        {
            sleep(1);
            pid_t rid = waitpid(id, &status, 1);
            if(rid > 0)
            {
                printf("等待子进程成功，退出码为%d\n",WEXITSTATUS(status));
                break;
            
            }
            else if(rid < 0)
            {
                printf("等待子进程失败\n");
                break;
            }
            else 
            {
                printf("子进程尚未退出\n");

                // 做自己的事情
                for(auto &task : tasks)
                {
                    task();
                }
            }
        }

    }



}

